﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Diagnostics;


public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";

        int displayimgid = Convert.ToInt32(context.Request.QueryString["ID"].ToString());
        
        System.IO.Stream strm = ShowDBImage(displayimgid);
       byte[] buffer = new byte[4096];
       int byteSeq = strm.Read(buffer, 0, 4096);
 
       while (byteSeq > 0)
       {
           context.Response.OutputStream.Write(buffer, 0, byteSeq);
           byteSeq = strm.Read(buffer, 0, 4096);
       }       
       
    }
    
    
    
    public System.IO.Stream ShowDBImage(int displayimid)
        {

            ABC_SA_DAL.Conexion conecta = new ABC_SA_DAL.Conexion();
            System.Data.SqlClient.SqlConnection conn = conecta.Conectar();
        
        //string conn = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer2"].ConnectionString;
        
        //System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(conn);
        
        string sql = "SELECT imagen FROM destino WHERE id_destino =@ID";
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, conn);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", displayimid);
        
        //connection.Open();
        object img = cmd.ExecuteScalar();
        try
        {
            return new System.IO.MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            conn.Close();
        }
    }
    
  
 
    public bool IsReusable {
        get {
            return false;
        }
    }
   
}